var register_prefix;

/**
 * setupRegisterPage
 * Sets up the register page's javascript so it can access the components' elements.
 * @param prefix String prefix for the component-name on the page
 */
function setupRegisterPage( prefix )
{
    register_prefix = prefix;
    
    window.onload = setupRegisterDocument;
}

/** 
 * setupRegisterDocument
 * Sets up events for the register page.
 */
function setupRegisterDocument() {
    document.getElementById(register_prefix + '_txtWachtwoord').onselect = cleanPasswordFields;
    cleanPasswordFields();
}

/**
 * cleanPasswordFields
 * Cleans the password fields and resets the isHashed field. We want it to be set
 * again when the user fills in a new set of password.
 */
function cleanPasswordFields() {
    document.getElementById(register_prefix + '_hfIsHashed').value = 0;
    document.getElementById(register_prefix + '_txtWachtwoord').value = "";
    document.getElementById(register_prefix + '_txtBevestigWachtwoord').value = "";
}

/**
 * validatePasswords
 * Validates the password fields and hashes it so we won't send un-encrypted passwords
 * over unsecure HTTP.
 * @param source
 * @param arguments
 */
function validatePasswords(source, arguments) {
    // Retreive the isHashed value.
    var isHashed = document.getElementById(register_prefix + '_hfIsHashed').value;
    
    if(0 == isHashed) {
        // Retreive the email and password values from the form.
        var email = document.getElementById(register_prefix + '_txtEmail').value;
        var password1 = document.getElementById(register_prefix + '_txtWachtwoord').value;
        var password2 = document.getElementById(register_prefix + '_txtBevestigWachtwoord').value;
        
        // If either one of the passwords is empty or the passwords do not match
        // we want the user to retreive an error.
        if(password1.length == 0 || password2.length == 0 || password1 != password2) {
            arguments.IsValid = false;
            return false;
        }
        
        // Hash the passwords and set the isHashed to 1 (true)
        document.getElementById(register_prefix + '_hfIsHashed').value = 1;
        document.getElementById(register_prefix + '_txtWachtwoord').value = hex_md5(email + password1);
        document.getElementById(register_prefix + '_txtBevestigWachtwoord').value = hex_md5(email + password2);
        
        arguments.IsValid = true;
    }
    return true;
}
